package com.uniledger.exchange.home.scheme;

import android.content.Context;
import android.text.TextUtils;

import com.uniledger.exchange.R;
import com.uniledger.exchange.home.HomeCenter;
import com.uniledger.exchange.model.OkSingleton;

import java.util.ArrayList;
import java.util.List;

public class PayDesc {

    public String label;
    public String desc;

    private PayDesc(String label, String desc) {
        this.label = label;
        this.desc = desc;
    }

    /**
     * 返回数据列表
     *
     * @param result
     * @return
     */
    public static List<PayDesc> payDescList(Context context,PayResult result) {
        if (result.flag == HomeCenter.SCHEME_TOP_UP) {
            return createChargeList(context,result);
        } else if (result.flag == HomeCenter.SCHEME_TRANSFER) {
            return createTransferList(context,result);
        } else if (result.flag == HomeCenter.SCHEME_WITHDRAW) {
            return createWithdrawList(context,result);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 转账成功
     *
     * @return
     */
    private static List<PayDesc> createTransferList(Context context,PayResult info) {
        List<PayDesc> list = new ArrayList<>();
        list.add(new PayDesc(getStr(context, R.string.pay_ok_transfer_to), info.receiver));
        list.add(new PayDesc(getStr(context, R.string.pay_ok_transfer_fee), info.getFee()));
        list.add(new PayDesc(getStr(context, R.string.pay_ok_transfer_hash),info.getHashStr()));
        return list;
    }

    private static String getStr(Context context, int id) {
        return context.getResources().getString(id);
    }

    /**
     * 充值成功
     *
     * @return
     */
    private static List<PayDesc> createChargeList(Context context,PayResult info) {
        List<PayDesc> list = new ArrayList<>();
        list.add(new PayDesc(getStr(context,R.string.pay_ok_topup_assets), info.balance + OkSingleton.getInstance().getCurrencyCode()));
        list.add(new PayDesc(getStr(context,R.string.pay_ok_topup_card), getSubCardNo(info)));
        list.add(new PayDesc(getStr(context,R.string.pay_ok_topup_remark), info.remark));
        list.add(new PayDesc(getStr(context,R.string.pay_ok_topup_hash),info.getHashStr()));
        return list;
    }

    private static String getSubCardNo(PayResult result) {
        String numStr = result.cardNo;
        if (!TextUtils.isEmpty(numStr) && numStr.length() > 4) {
            return result.bankCard + "****" + numStr.substring(numStr.length() - 4, numStr.length());
        } else {
            return result.bankCard + "****" + numStr;
        }
    }

    /**
     * 提现成功
     *
     * @return
     */
    private static List<PayDesc> createWithdrawList(Context context,PayResult info) {
        List<PayDesc> list = new ArrayList<>();
        list.add(new PayDesc(getStr(context,R.string.pay_ok_withdraw_card), getSubCardNo(info)));
        list.add(new PayDesc(getStr(context,R.string.pay_ok_withdraw_amount), info.amount + OkSingleton.getInstance().getCurrencyCode()));
        list.add(new PayDesc(getStr(context,R.string.pay_ok_withdraw_arrival), info.arriveAmount + info.arriveCurrency));
        list.add(new PayDesc(getStr(context,R.string.pay_ok_withdraw_fee), info.getFee()));
        list.add(new PayDesc(getStr(context,R.string.pay_ok_withdraw_rate), getRateStr(info)));
        list.add(new PayDesc(getStr(context,R.string.pay_ok_withdraw_hash),info.getHashStr()));
        return list;
    }

    private static String getRateStr(PayResult info) {
        String string = "1 %1$s = %2$s %3$s";
        return String.format(string, OkSingleton.getInstance().getCurrencyCode(), info.rate, info.arriveCurrency);
    }
}
